class User < ApplicationRecord
  has_secure_password

  validates :phone, presence: true, uniqueness: true
  validates :nickname, presence: true, uniqueness: true

  has_many :bets
  has_many :plans

  def bet? game_id
    bet = self.bets.where(game_id: game_id).first
    if bet.blank?
      0
    else
      if bet.master_or_guest
        1
      else
        2
      end
    end
  end

  def plans_sum
    total_fee = 0
    self.plans.each do |plan|
      total_fee += (plan.win_money - plan.lose_money)
    end
    total_fee
  end
  
end
